package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.WebSettings;
import android.widget.EditText;
import android.widget.TextView;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.ui.views.webview.BridgeHandler;
import com.captainbank.tcl.tclapplication.ui.views.webview.BridgeWebView;
import com.captainbank.tcl.tclapplication.ui.views.webview.BridgeWebViewClient;
import com.captainbank.tcl.tclapplication.ui.views.webview.CallBackFunction;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.StringUtil;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by TCL on 2019/8/7.
 * Describe:
 */
public class JsWebviewActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.tv_show)
    TextView tvShow;
    @BindView(R.id.webview)
    BridgeWebView webView;

    private Handler mHandler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_js_webview;
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_jswebview));

        WebSettings ws = webView.getSettings();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
//        }
        // 垂直方向不显示进度条
        webView.setVerticalScrollBarEnabled(false);
        webView.setWebViewClient(new MyWebViewClient(webView));
//        webView.setDefaultHandler(new MyHadlerCallBack());//默认交互
        webView.setWebChromeClient(new WebChromeClient());
        // 加载本地网页
        webView.loadUrl("file:///android_asset/demo.html");
        // 加载服务器网页
//        webView.loadUrl("http://html5test.com");


        webView.registerHandler("methodInJava", new BridgeHandler() {

            @Override
            public void handler(String data, CallBackFunction function) {
//                int code = 0;
//                String objString = null;
//                if (StringUtil.checkIsNotNull(data)) {
//                    try {
//                        JSONObject jObject = new JSONObject(data);
//                        code = jObject.getInt("code");
//                        objString = jObject.getJSONObject("obj").toString();
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
                MyToast.toastNormal(JsWebviewActivity.this, data);
                tvShow.setText("TCL------(长按删除)");

                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        function.onCallBack("JS调用Android成功");
                    }
                }, 3000);

//                }
            }

        });
    }

    @Override
    protected void setListener() {
        tvShow.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                tvShow.setText("");
                return true;
            }
        });
    }

    @OnClick({R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn:
                String input = et.getText().toString().trim();
                if (StringUtil.checkIsNotNull(input)) {
                    // 调用js
                    webView.callHandler("showMsg", input, new CallBackFunction() {
                        @Override
                        public void onCallBack(String data) {
                            MyToast.toastNormal(JsWebviewActivity.this, data);
                        }
                    });
                } else {
                    MyToast.toastNormal(JsWebviewActivity.this, "请输入内容");
                }
                break;
        }
    }

    class MyWebViewClient extends BridgeWebViewClient {

        public MyWebViewClient(BridgeWebView webView) {
            super(webView);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("http:") || url.startsWith("https:")) {
                view.loadUrl(url);
                return true;
            } else {
                return super.shouldOverrideUrlLoading(view, url);
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

    }

}
